package com.guaniu.voip.ui.main;

import static com.guaniu.voip.sip.SipManager.SIP_CALL_OUTGOING;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.viewpager.widget.ViewPager;

import com.guaniu.voip.BaseActivity;
import com.guaniu.voip.R;
import com.guaniu.voip.sip.Constant;
import com.guaniu.voip.sip.SipManager;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;

import net.gotev.sipservice.BroadcastEventReceiver;
import net.gotev.sipservice.MediaState;

public class MainActivity extends BaseActivity  implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener , BroadcastEventReceiver.SipEventObserver, View.OnClickListener {
    private QMUITopBarLayout mTopBar;

    //UI Objects
    private RadioGroup rg_tab_bar;
    private RadioButton rb_channel;
    private RadioButton rb_message;
    private RadioButton rb_better;
    private RadioButton rb_setting;
    private ViewPager vpager;

    private MyFragmentPagerAdapter mAdapter;

    //几个代表页面的常量
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;
    public static final int PAGE_FOUR = 3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        bindViews();
        rb_channel.setChecked(true);

        //初始化voip监听
        BroadcastEventReceiver.registerEventObserver(this, this);
    }

    private void bindViews() {
        rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        rb_channel = (RadioButton) findViewById(R.id.rb_channel);
        rb_message = (RadioButton) findViewById(R.id.rb_dialkeypad);
        rb_setting = (RadioButton) findViewById(R.id.rb_setting);
        rg_tab_bar.setOnCheckedChangeListener(this);
        rb_channel.setOnClickListener(this);
        rb_message.setOnClickListener(this);
        rb_setting.setOnClickListener(this);

        vpager = (ViewPager) findViewById(R.id.vpager);
        vpager.setAdapter(mAdapter);
        vpager.setCurrentItem(0);
        vpager.addOnPageChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_channel:
                vpager.setCurrentItem(PAGE_ONE);
                break;
            case R.id.rb_dialkeypad:
                vpager.setCurrentItem(PAGE_TWO);
                break;
            case R.id.rb_setting:
                vpager.setCurrentItem(PAGE_THREE);
                break;
        }
    }

    //重写ViewPager页面切换的处理方法
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        BroadcastEventReceiver.getBroadcastEventReceiver().unRegisterEventObserverInner(this);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //state的状态有三个，0表示什么都没做，1正在滑动，2滑动完毕
        if (state == 2) {
            switch (vpager.getCurrentItem()) {
                case PAGE_ONE:
                    rb_channel.setChecked(true);
                    break;
                case PAGE_TWO:
                    rb_message.setChecked(true);
                    break;
                case PAGE_THREE:
                    rb_setting.setChecked(true);
                    break;
                case PAGE_FOUR:
                    rb_setting.setChecked(true);
                    break;
            }
        }
    }


    private void initView() {
        mTopBar = findViewById(R.id.topbar);
        mTopBar.setTitle("SIP DEMO");
    }


    @Override
    public void notifyRegState(String account, int code, String reason, long expiration) {

    }

    @Override
    public void notifyIncomingCall(String accountID, int callID, String displayName, String remoteUri, boolean isVideo) {

        Intent intent = new Intent(MainActivity.this, TryingCallActivity.class);
        int media_type = (isVideo?Constant.MEDIA_TYPE_VIDEO:Constant.MEDIA_TYPE_AUDIO);
        intent.putExtra(Constant.MEDIA_TYPE, media_type);
        SipManager.getInstance().setOutGoingVideo(isVideo);
        intent.putExtra(Constant.IS_MAKE_CALL, false);
        intent.putExtra(Constant.CALL_NUMBER, accountID);
        intent.putExtra(Constant.SESSION_ID, callID);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public void notifyCallState(String accountID, int callID, int callStateCode, int callStatusCode, long connectTimestamp) {

    }

    @Override
    public void notifyCallMediaState(String accountID, int callID, MediaState stateType, boolean stateValue) {

    }

    @Override
    public void notifyChangeNetwork() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rb_channel:
                vpager.setCurrentItem(PAGE_ONE);
                break;
            case R.id.rb_dialkeypad:
                vpager.setCurrentItem(PAGE_TWO);
                break;
            case R.id.rb_setting:
                vpager.setCurrentItem(PAGE_THREE);
                break;
        }
    }
}
